﻿using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using YiSah.Entity.Extension.Systen;
using YiSha.Admin.Web.Controllers;
using YiSha.Basics;
using YiSha.Entity.SystemManage;
using YiSha.Enums;
using YiSha.Model;
using YiSha.Model.Param.SystemManage;
using YiSha.Service.SystemManage;
using YiSha.Util.Model;

namespace YiSha.Admin.Web.Areas.SystemManage.Controllers
{
    [Area("SystemManage")]
    public class RoleController : BaseController
    {
        public RoleService RoleService { get; set; }
        #region 视图功能
        [AuthorizeFilter("system:role:view")]
        public IActionResult RoleIndex()
        {
            return View();
        }

        public IActionResult RoleForm()
        {
            return View();
        }
        #endregion

        #region 获取数据
        [HttpGet]
        [AuthorizeFilter("system:role:search,organization:user:search")]
        public async Task<IActionResult> GetListJson(RoleListParam param)
        {
            TData<List<RoleEntityExtension>> obj = await RoleService.GetList(param);
            return Json(obj);
        }

        [HttpGet]
        [AuthorizeFilter("system:role:search,organization:user:search")]
        public async Task<IActionResult> GetPageListJson(RoleListParam param, Pagination pagination)
        {
            var obj = await RoleService.GetPageList(param, pagination);
            return Json(obj);
        }

        [HttpGet]
        public async Task<IActionResult> GetFormJson(string id)
        {
            TData<RoleEntityExtension> obj = await RoleService.GetEntity(id);
            return Json(obj);
        }

        [HttpGet]
        public async Task<IActionResult> GetRoleName(RoleListParam param)
        {
            TData<string> obj = new TData<string>();
            var list = await RoleService.GetList(param);
            if (list.MsgCode == MsgCode.Success)
            {
                obj.Data = string.Join(",", list.Data.Select(p => p.RoleName));
            }
            return Json(obj);
        }

        [HttpGet]
        public async Task<IActionResult> GetMaxSortJson()
        {
            TData<int> obj = await RoleService.GetMaxSort();
            return Json(obj);
        }
        #endregion

        #region 提交数据
        [HttpPost]
        [AuthorizeFilter("system:role:add,system:role:edit")]
        public async Task<IActionResult> SaveFormJson(RoleEntityExtension entity)
        {
            TData<string> obj = await RoleService.SaveForm(entity);
            return Json(obj);
        }
        [HttpPost]
        [AuthorizeFilter("system:role:edit")]
        public async Task<IActionResult> ChangeStatus(string roleId)
        {
            TData obj = await RoleService.ChangeStatus(roleId);
            return Json(obj);
        }
        [HttpPost]
        [AuthorizeFilter("system:role:delete")]
        public async Task<IActionResult> DeleteFormJson(List<string> ids)
        {
            TData obj = await RoleService.DeleteForm(ids);
            return Json(obj);
        }
        #endregion
    }
}